package lib

import (
	"strings"
)

func init() {
	Probs = append(Probs, Problem{
		Num:         394,
		Discription: "压缩字符串解码",
		Level:       2,
		Labels: map[string]int{
			"栈":  1,
			"双栈": 1,
		},
	})
}

// 遇到数字正常加数位，遇到字符正常加字符串
// 遇到 [ 将数字和字符串都进栈，重置数字和当前字符串
// 遇到 ] 将当前的字符串重复数字栈栈顶的次数，然后双双出栈，在字符串栈顶的后面添加重复后的当前字符串
func DecodeString(s string) string {
	curNum := 0
	curStr := ""
	numS := make([]int, 0)
	strS := make([]string, 0)
	for i := range s {
		if s[i] >= '0' && s[i] <= '9' {
			curNum = curNum*10 + int(s[i]-'0')
			continue
		}

		if s[i] >= 'a' && s[i] <= 'z' {
			curStr += string(s[i])
			continue
		}

		if s[i] == '[' {
			//进入下一层字符串
			numS = append(numS, curNum)
			curNum = 0
			strS = append(strS, curStr)
			curStr = ""
		} else {
			//把当前字符串重复上一层的count次，在上一层的字符串后面添加
			curStr = strS[len(strS)-1] + strings.Repeat(curStr, numS[len(numS)-1])
			numS = numS[:len(numS)-1]
			strS = strS[:len(strS)-1]
		}
	}

	return curStr
}
